package norain.wxt.controllers;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.MessageFormat;
import java.util.Properties;

/**
 * Created by wxt on 16/4/21.
 */
@WebServlet(name = "HelloJdbc")
public class HelloConfigs extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setCharacterEncoding("UTF-8");//设置将字符以"UTF-8"编码输出到客户端浏览器
        //通过设置响应头控制浏览器以UTF-8的编码显示数据，如果不加这句话，那么浏览器显示的将是乱码
        response.setHeader("content-type", "text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();
        try {
            //获取web目录下的db.properties配置

            GetWebConfigs(out);
            out.write("<hr/>");
            GetSrcConfigs(out);
            out.write("<hr/>");
            GetPackageConfigs(out);
            out.write("<hr/>");
        } catch (Exception ex) {
            out.write(("发生错误:" + ex.getMessage()));
        } finally {

        }

    }

    private void GetWebConfigs(PrintWriter out) throws IOException {

        InputStream in = this.getServletContext().getResourceAsStream("/db_new.properties");
        Properties prop = new Properties();
        prop.load(in);
        String dbName = GetPropertyValue(prop, "dbName");
        String dbName2 = GetPropertyValue(prop, "dbName2");

        out.println("读取到web目录下的db.properties文件：");

        out.println(
                MessageFormat.format(
                        "dbName={0},dbName2={1},username={2},password={3}",
                        dbName, dbName2));
    }

    private void GetSrcConfigs(PrintWriter out) throws IOException {

        InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db1.properties");

        Properties prop = new Properties();
        prop.load(in);


        String driver = GetPropertyValue(prop, "driver");
        String url = GetPropertyValue(prop, "url");
        String username = GetPropertyValue(prop, "username");
        String password = GetPropertyValue(prop, "password");

        out.println("读取到Src根目录下的db.properties文件：");

        out.println(
                MessageFormat.format(
                        "driver={0},url={1},username={2},password={3}",
                        driver, url, username, password));
    }

    private void GetPackageConfigs(PrintWriter out) throws IOException {

        //方法一
        // InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/norain/wxt/config/server.properties");


        //方法二 通过ServletContext获取web资源的绝对路径
        String path = this.getServletContext().getRealPath("/WEB-INF/classes/norain/wxt/config/server.properties");
        InputStream in = new FileInputStream(path);

        Properties prop = new Properties();
        prop.load(in);


        String serverName = GetPropertyValue(prop, "serverName");
        String serverName2 = GetPropertyValue(prop, "serverName2");

        out.println("读取到norain/wxt/config包目录下的server.properties文件：");

        out.println(
                MessageFormat.format(
                        "driver={0},url={1}",
                        serverName, serverName2));
    }

    private static String GetPropertyValue(Properties prop, String key) throws UnsupportedEncodingException {
        String valueStr = prop.getProperty(key);
        valueStr = new String(valueStr.getBytes("ISO8859-1"), "UTF-8");//获取request对象以ISO8859-1字符编码接收到的原始数据的字节数组，然后通过字节数组以指定的编码构建字符串，解决乱码问题
        return valueStr;
    }
}
